package com.yanqu.road.server.cmd.mail;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.pb.mail.MailProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.mail.MailModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_MAIL_DELETE_ALL_MAIL, desc = "一键删除可删除邮件")
public class DeleteAllMailCmd extends AbstractCommand {

	int mailType;

	// 旧版本无类型区分，客户端没传类型表示全部
	boolean checkType = true;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		MailProto.MailDeleteAllReqMsg req = MailProto.MailDeleteAllReqMsg.parseFrom(packet.getBytes());
		if (req.hasMailType()) {
			mailType = req.getMailType();
		} else {
			checkType = false;
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		List<Integer> typeList = new ArrayList<>();
		typeList.add(mailType);

		if (checkType && mailType == eMailType.SYSTEM.getValue()) {
			typeList.add((int) eMailType.MARKETING.getValue());
		}

		ret = player.getModule(MailModule.class).deleteAllMailByType(checkType, typeList);
	}

	@Override
	public void sendData(GamePlayer player) {
		MailProto.MailDeleteAllRespMsg.Builder respMsg = MailProto.MailDeleteAllRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_MAIL_DELETE_ALL_MAIL, respMsg);
	}

}
